package sk.naive.talker.command;

import sk.naive.talker.*;
import sk.naive.talker.props.PropertyStoreException;
import sk.naive.talker.message.*;
import sk.naive.talker.util.Buffer;

import java.rmi.RemoteException;
import java.util.Collection;

/**
 *
 * @author <a href="mailto:rytier@naive.deepblue.sk">Martin "Rytier" Kerni</a>
 * @version $Revision: 1.10 $ $Date: 2005/01/19 22:41:26 $
 */
public abstract class AddressedLocalMessageCommand extends AddressedPublicMessageCommand {
	protected final void execSend(User recipient, String s) throws RemoteException, CommandException, PropertyStoreException {
		if (new LocationHelper(commandDispatcher).locationForUser(user) == null) {
			sendHelper().sendMessage(user, "message.noRoom");
			return;
		}
		super.execSend(recipient, s);
	}

	protected String targetNotFoundWarning() {
		return "message.recipientNotHere";
	}

	protected void performTargetNotFoundWarning(User user, String recipientLogin) throws RemoteException, PropertyStoreException {
		User found = commandDispatcher.getUserFinder().findLoggedUser(recipientLogin);
		if (found == null) {
			user.getProperties().put(DefaultMessageFactory.CTXKEY_VAL, recipientLogin);
			sendHelper().sendMessage(user, notLoggedInWarning());
		} else {
			super.performTargetNotFoundWarning(user, (String) found.get(User.UPROP_NAME));
		}
	}

	protected Collection<User> getPossibleTargetUsers(User user) {
		return new LocationHelper(commandDispatcher).locationForUser(user).users();
	}

	protected Buffer getBuffer(User user) throws CommandException {
		return new LocationHelper(commandDispatcher).locationBufferForUser(this);
	}

	protected Category getCategory() {
		return Category.SAY;
	}
}
